package com.dg.river.netty.auditdev;



import com.dg.river.netty.axle.net.DeviceMsgDecoder;
import com.dg.river.netty.axle.net.DeviceMsgEncoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

public class AuditDevClientInitializer extends ChannelInitializer<SocketChannel> {

    private final static int readerIdleTimeSeconds = 60 * 10;//读操作空闲
    private final static int writerIdleTimeSeconds = 60 * 9;//写操作空闲
    private final static int allIdleTimeSeconds = 60 * 10;//读写全部空闲

    /**
     * 客户端
     */
    private AuditDevClient client;

    public AuditDevClientInitializer(AuditDevClient auditDevClient) {
        this.client = auditDevClient;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline p = socketChannel.pipeline();

//        SSLEngine engine = SslContextFactory.getClientContext().createSSLEngine();
//        engine.setUseClientMode(true);
//        p.addFirst("ssl", new SslHandler(engine));
        p.addLast("idle", new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds));
        p.addLast("decoder", new DeviceMsgDecoder());
        p.addLast("encoder", new DeviceMsgEncoder());

        //and then business logic.
        p.addLast("handler", new AuditDevClientHandler(client));
    }
}
